<?php
/**
 * @copyright: ©2024 永州 微聚商网络科技有限公司
 * @link: https://www.wEijuyunke.com
 */
namespace app\api\controller\consume;

use app\admin\model\FigureIndexSet;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
use app\admin\model\FigureSystem;
use EasyAdmin\tool\CommonTool;
use app\admin\model\FigureBanner;

/**
 * @ControllerAnnotation(title="轮播图管理")
 */
class Banner extends AdminController
{

    use \app\admin\traits\Curd;

    /**
     * 字段排序
     *
     * @var array
     */
    protected $sort = [
        'sort' => 'desc'
    ];

    /**
     * 允许修改的字段
     *
     * @var array
     */
    protected $allowModifyFields = [
        'is_display',
        'sort'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Banner();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $request = $this->request;
        $param = $request->param();
        $page = input("page");
        $limit = input("limit");
        $where = [];
        if ($this->request->platform_id >0 ){
            $where[] = ['platform_id','=',$this->request->platform_id];
        }

        if (! empty(input('b_type'))) {
            $where[] = [
                "b_type",
                "=",
                input('b_type')
            ];
        }

        if (! empty(input('mold'))) {
            $where[] = [
                "mold",
                "=",
                input('mold')
            ];
        }
        if (isset( $param['platform_name']) &&   $param['platform_name'] ){
            $where[] = ['platformInfo.name','=',$param['platform_name']];
        }
        $count = $this->model->withJoin('platformInfo','left')
            ->where($where)->count();
        $list = $this->model->withJoin('platformInfo','left')->where($where)
            ->page($page, $limit)
            ->order($this->sort)
            ->select();

        return msg([
            'list' => $list,
            'count' => $count
        ], 0, "success");
    }



    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);

            if (empty($post['pic_url'])){
                return  msg([], 1, "请上传轮播图") ;
            }
            $post['platform_id'] = $this->request->platform_id ;
            try {
                $post['url'] = str_replace("&amp;", "&", html_entity_decode($post['url']));
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                return msg([], 1, "保存异常" . $e->getMessage());
            }
            return $save ? msg([], 0, "保存成功") : msg([], 1, "保存失败");
        }
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $post['url'] = str_replace("&amp;", "&", html_entity_decode($post['url']));
               // $post['platform_id'] = $post['platform_id'];
                $save = $row->save($post);
            } catch (\Exception $e) {
                return msg([], 1, "保存异常" . $e->getMessage());
            }
            return $save ? msg([], 0, "保存成功") : msg([], 1, "保存失败");
        }
    }

    /**
     * 拖动排序
     * @NodeAnotation(title="拖动排序")
     */
    public function sortChange()
    {
        $request = $this->request;

        Db::startTrans();

        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $data = $post['params'];
                $sort_number = count($data);
                foreach ($data as $k => $v) {
                    $this->model->where("id", $v['id'])->update(array(
                        "sort" => $sort_number
                    ));
                    $sort_number --;
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return msg([], 1, "保存失败" . $e->getMessage());
            }
            return msg([], 0, "保存成功");
        }
    }
}